read_teacher_data<-function(){
  setwd(wd_data_raw)
  setwd("./teacher/")
  data_permanent_exam<-readRDS("data_permanent_exam")
  data_permanent_transfer<-readRDS("data_permanent_transfer")
  data_temp_transfer<-readRDS("data_temp_transfer")
  data_temp_exam<-readRDS("data_temp_exam")
  
  
  data_teacher<-bind_rows(list(data_permanent_exam,data_permanent_transfer,data_temp_transfer,data_temp_exam), .id='source') %>% arrange(Teacher,Year)
  data_teacher<-data_teacher %>% 
    mutate(source=ifelse(source==1,'perm_exam',source)) %>% 
    mutate(source=ifelse(source==2,'perm_trans',source)) %>%
    mutate(source=ifelse(source==3,'temp_trans',source)) %>% 
    mutate(source=ifelse(source==4,'temp_exam',source))
  
  perc.rank <- function(x) {
    y<-rank(x)/length(x)
    return(y)}

  #Get percentile entrance grade, excluding NA's
  data_teacher<-as.data.frame(data_teacher)
  data_teacher$teacher_perc<-NA
  data_teacher[!is.na(data_teacher$Grade_Clean) & data_teacher$Grade_Clean!="-" ,]<-data_teacher[!is.na(data_teacher$Grade_Clean) & data_teacher$Grade_Clean!="-",] %>%
   # group_by(Year,Subject_Category) %>%
    mutate(teacher_perc=perc.rank(as.numeric(Grade_Clean))) %>%
    ungroup
  
  data_teacher$gpa_perc<-NA
  data_teacher[!is.na(data_teacher$gpa) & data_teacher$gpa!="-" ,]<-data_teacher[!is.na(data_teacher$gpa) & data_teacher$gpa!="-",] %>%
    # group_by(Year,Subject_Category) %>%
    mutate(gpa_perc=perc.rank(as.numeric(gpa))) %>%
    ungroup
  
  
  setwd(wd_data_final)
  saveRDS(data_teacher,'data_teacher')

  return(data_teacher)
}